<!--
 * @Description: 资讯评论审核
 * @Author: Liulian
 * @Date: 2021-06-24 17:16:30
 * @LastEditors: Morgan
 * @LastEditTime: 2021-07-02 19:53:50
-->
<template>
  <!-- 资讯评论审核 -->
  <div>
    <!-- 模糊搜索 -->
    <div class="search">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item>
          <el-button type="danger" @click="toBatchDelete">批量删除</el-button>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="searchForm.keywords"
            placeholder="请输入关键词"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 评论管理展示 -->
    <div>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="序号" type="index" align="center" />
        <el-table-column prop="comment" label="评论内容" align="center" />
        <el-table-column prop="userId" label="评论人编号" align="center" />
        <el-table-column prop="articleId" label="资讯编号" align="center" />
        <el-table-column prop="commentTime" label="评论时间" align="center">
          <template slot-scope="scope">
            {{ moment(scope.row.commentTime).format("YYYY-MM-DD HH:mm:ss") }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="评论状态" align="center">
          <template slot-scope="scope">
            <!-- <el-tag :type="scope.row.status === '通过' ? 'success' : 'danger'" > -->
            <!-- <el-tag> -->
            <el-tag v-if="scope.row.status === '通过'" type="success">{{
              scope.row.status
            }}</el-tag>
            <el-tag v-else-if="scope.row.status === '不通过'" type="danger">{{
              scope.row.status
            }}</el-tag>
            <el-tag v-else type="info">{{ scope.row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140">
          <template slot-scope="scope">
            <el-button
              class="red_info"
              type="text"
              size="small"
              @click="toDelete(scope.row.id)"
            >删除</el-button>
            <a
              v-if="scope.row.status == '未审核'"
              type="text"
              class="green_info"
              @click.prevent="toChengeStatus(scope.row, '通过')"
            >通过</a>
            <a
              v-if="scope.row.status == '未审核'"
              type="text"
              class="wanring_info"
              @click.prevent="toChengeStatus(scope.row, '不通过')"
            >不通过</a>
            <!-- <el-button
              class="success_info"
              type="text"
              size="small"
              :disabled="scope.row.status === '通过' || scope.row.status === '不通过'"
              @click="toPass(scope.row.id)"
            >通过</el-button>
            <el-button
              class="danger_info"
              type="text"
              size="small"
              :disabled="scope.row.status === '通过' || scope.row.status === '不通过' "
              @click="toNoPass(scope.row.id)"
            >不通过</el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="pagination-div">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[2, 5, 10, 15, 20]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import moment from 'moment'
import { pageQuery, deleteById, batchDelete, check } from '@/api/infoComment'
export default {
  data() {
    return {
      // 模糊搜索表单的数据
      searchForm: {
        keywords: ''
      },
      // 表格展示的数据
      tableData: [],
      // 分页参数
      params: {
        page: 1,
        pageSize: 10
      },
      ids: [],
      // 总条数
      total: 0,
      // 加载
      loading: false,
      pass: {
        status: '通过'
      },
      nopass: {
        status: '不通过'
      }
    }
  },
  computed: {},
  watch: {
    params: {
      handler() {
        this.queryData()
      },
      deep: true
    }
  },
  created() {
    // 发请求
    this.queryData()
  },
  mounted() {},
  methods: {
    moment,
    // 获取分页订单评论数据
    async queryData() {
      this.loading = true
      // const res = await pageQuery({ page: 1, pageSize: 100 })
      const temp = {
        // 合并数据
        ...this.params,
        ...this.searchForm
      }
      // 遍历对象
      for (const key in temp) {
        // key是一个变量，代表的属性名
        // console.log(key, temp[key]);
        if (!temp[key]) {
          delete temp[key]
        }
      }
      const res = await pageQuery(temp)
      this.loading = false
      this.tableData = res.data.list
      // res.data.list = moment(scope.row.publishTime).format("YYYY-MM-DD")
      this.total = res.data.total
    },
    toSearch() {
      this.queryData()
    },
    // 每页显示的条数发生变化时的处理程序
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      // this.params.pageSize = val;
      // this.params.page = 1;
      this.params = {
        pageSize: val,
        page: 1
      }
    },
    // 当前页码发生变化时的事件处理程序
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.params.page = val
    },
    // 删除资讯评论
    async toDelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await deleteById({ id })
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.queryData()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
          this.queryData()
        })
    },
    handleSelectionChange(val) {
      this.ids = val.map((item) => item.id)
    },
    async toBatchDelete(ids) {
      // let data = this.multipleSelection
      // let res = await batchDelete({ids:this.ids})
      // this.$notify.success({
      //   title: '成功',
      //   message: res.success
      // })
      // this.queryData()
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await batchDelete({ ids: this.ids })
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.queryData()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
          this.queryData()
        })
    },
    // 审核
    async toChengeStatus(row, status) {
      const temp = {
        id: row.id,
        status
      }
      const res = {}
      if (status == '通过') {
        const res = await check(temp)
      } else {
        const res = await check(temp)
      }
      this.$notify.success({
        title: '成功',
        message: res.message
      })
      this.queryData()
    }
    // 审核评论
    // async toPass(id) {
    //   const temp = {
    // 合并数据
    //   id,
    //   ...this.pass,
    // };
    // const res = await check(temp);
    // res.status = '审核通过'
    //   this.$notify.success({
    //     title: "成功",
    //     message: res.message,
    //   });
    //   this.queryData();
    // },
    // async toNoPass(id) {
    // const temp = {
    // 合并数据
    //   id,
    //   ...this.nopass,
    // };
    // const res = await check(temp);
    // res.status = '审核通过'
    // this.$notify.success({
    //   title: "成功",
    //   message: res.message,
    // });
    // this.queryData();
    // },
  }
}
</script>
<style scoped>
</style>
